/*
 * @Author: song
 * @Description:红点系统
 * @Date: 2024年04月14日 星期日 17:04:23
 * @Modify:
 */

using System;
using System.Collections.Generic;
using SCore.Util;
using UnityEngine;

namespace Function.RedPoint
{
    public partial class RedPointSystem : Singleton<RedPointSystem>
    {
        [Tooltip("root")] private RedPointNode _root;

        [Tooltip("red Map 加速 查找节点速度 按树结构需要遍历查找")]
        private Dictionary<SRedPointType, RedPointNode> _redPoints = new Dictionary<SRedPointType, RedPointNode>();

        public override void Init()
        {
            //配置表
            InitConfig();
        }

        public override void Clear()
        {
            _root = null;
            _redPoints.Clear();
        }

        /// <summary>
        /// 得到节点
        /// </summary>
        /// <param name="pointType">类型</param>
        /// <param name="redPointNode">结果</param>
        /// <returns></returns>
        private bool GetNode(SRedPointType pointType, out RedPointNode redPointNode)
        {
            if (!_redPoints.TryGetValue(pointType, out redPointNode))
            {
                ($"[RedPointSystem]:{pointType} 不存在").Error();
                return false;
            }

            return true;
        }

        /// <summary>
        /// 增加红点配置
        /// </summary>
        /// <param name="pointType">红点</param>
        /// <param name="numFunc">计数回调</param>
        /// <param name="parent">父节点</param>
        /// <returns></returns>
        private RedPointNode AddRedPoint(SRedPointType pointType, Func<int> numFunc = null,
            RedPointNode parent = null)
        {
            RedPointNode redPointNode = new RedPointNode()
            {
                _nodeType = pointType,
                _parent = parent,
                _num = 0,
                _numFunc = numFunc,
            };
            if (parent == null)
            {
                if (_root != null)
                {
                    $"[RedPointSystem]:{pointType} error:_root is exist AddRedPoint Fair ".Error();
                    return null;
                }

                _root = redPointNode;
            }
            else
            {
                parent._childs.Add(redPointNode);
            }

            _redPoints.Add(pointType, redPointNode);
            return redPointNode;
        }


        //public
        //view注册
        public RedPointNode AddRedPoint(RedPointView redPointView)
        {
            if (GetNode(redPointView._pointType, out var node))
            {
                node._viewAction.Add(redPointView.Execute);
            }

            return node;
        }

        //view释放
        public void RemoveRedPoint(RedPointView redPointView)
        {
            if (GetNode(redPointView._pointType, out var node))
            {
                node._viewAction.Remove(redPointView.Execute);
            }
        }

        //刷新计数
        public void RefreshRedPoint(SRedPointType redPointType)
        {
            if (GetNode(redPointType, out var node))
            {
                if (node.Leaf())
                {
                    node.SetNum();
                }
            }
        }
    }
}